//
//  Game.m
//  hanafudaIPhone
//
//  Created by 우성 김 on 12. 3. 26..
//  Copyright (c) 2012년 yy01ar@gmail.com. All rights reserved.
//

#import "Game.h"
#import "Deck.h"
#import "Line.h"
#import "Card.h"
#import "CCTouchDispatcher.h"

@implementation Game

-(void)put
{    
    // 놓을 수 있으면
    if([putDeck canPut]) {
        while(![lines[nextLine] isEnable])
        {
            nextLine = (nextLine + 1) % 5;
        }

        Card* card = [putDeck put ];
        [lines[nextLine] addCard:card];
        
        // 뗄 수 있으면
        if([lines[nextLine] canPop]) 
        {
            NSMutableArray* popCardList = [lines[nextLine] popCard];
            [popDeck addCardList:popCardList];
        }
    }
    else {
        putDeck = popDeck;
        popDeck = [Deck createEmptyDeck];
    }
}

// TODO Not implemented
-(int)getState
{
    return 0;
}

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	Game *layer = [Game node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

-(CCSprite *)spriteWithColor:(ccColor4F)bgColor textureSize:(float)textureSize {
    CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:textureSize];
    [rt beginWithClear:bgColor.r g:bgColor.g b:bgColor.b a:bgColor.a];
    [rt end];
    
    return [CCSprite spriteWithTexture:rt.sprite.texture];
}

-(void)genBackground 
{
    [_background removeFromParentAndCleanup:YES];
    
    ccColor4F bgColor = ccc4FFromccc4B(ccc4(13, 88, 15, 255));
    _background = [self spriteWithColor:bgColor textureSize:1024];
    
    CGSize winSize = [CCDirector sharedDirector].winSize;
    _background.position = ccp(winSize.width/2, winSize.height/2);
    [self addChild:_background z:-1];
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
		[self genBackground];
        
        nextLine = 0;
        
        lines[0] = [[Line alloc]init];
        lines[1] = [[Line alloc]init];
        lines[2] = [[Line alloc]init];
        lines[3] = [[Line alloc]init];
        lines[4] = [[Line alloc]init];
        
        putDeck = [Deck createInitialDeck];
        popDeck = [Deck createEmptyDeck];
        
        //		// create and initialize a Label
        //		CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
        //
        //		// ask director the the window size
        //		CGSize size = [[CCDirector sharedDirector] winSize];
        
        //Deck* deck = [Deck createInitialDeck];
        
        // Test Code
        int i = 0;
        for(Card* card in putDeck.cardArray)
        {
            CCSprite* sprite = card;
            sprite.position = ccp (100 + (i / 4) * 100, 640 - (i % 4) * 200);
            [self addChild:sprite];
            i++;
        }
        
        self.isTouchEnabled = YES;
	}
	return self;
}

-(void)registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    return YES;
}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    [self put]; 
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
